package connection
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import mx.containers.Canvas;
	import mx.containers.Panel;
	import mx.core.Application;
	import mx.core.SpriteAsset;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	
	
	public class PanelRoom extends Panel
	{
		public var gdr:Salle;
		public var salle:String;
		
		// Add the creationCOmplete event handler.
		public function PanelRoom(st:String)
		{
			salle = st;
			gdr = new Salle(st);
			gdr.setScale(0.5);
			gdr.lireFichier(st+".xml");
			this.addChild(gdr);
			
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
		}
		
		// Expose the title bar property for draggin and dropping.
		[Bindable]
		public var myTitleBar:UIComponent;
		
		private function creationCompleteHandler(event:Event):void
		{
			myTitleBar = titleBar;			
			// Add the resizing event handler.	
			//addEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
		}
		
		
		protected var minShape:SpriteAsset;
		protected var restoreShape:SpriteAsset;
		protected var closeShape:SpriteAsset;
		protected var zoomPlusShape:SpriteAsset;
		protected var zoomMoinsShape:SpriteAsset;
		protected var zoomNormalShape:SpriteAsset;
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			// Create the SpriteAsset's for the min/restore icons and 
			// add the event handlers for them.
			minShape = new SpriteAsset();
			minShape.addEventListener(MouseEvent.MOUSE_DOWN, minPanelSizeHandler);
			titleBar.addChild(minShape);
			
			restoreShape = new SpriteAsset();
			restoreShape.addEventListener(MouseEvent.MOUSE_DOWN, restorePanelSizeHandler);
			titleBar.addChild(restoreShape);
			
			closeShape = new SpriteAsset();
			closeShape.addEventListener(MouseEvent.MOUSE_DOWN, closePanelSizeHandler);
			titleBar.addChild(closeShape);
			
			zoomPlusShape = new SpriteAsset();
			zoomPlusShape.addEventListener(MouseEvent.MOUSE_DOWN, zoomPlusPanelSizeHandler);
			titleBar.addChild(zoomPlusShape);
			
			zoomMoinsShape = new SpriteAsset();
			zoomMoinsShape.addEventListener(MouseEvent.MOUSE_DOWN, zoomMoinsPanelSizeHandler);
			titleBar.addChild(zoomMoinsShape);
			
			zoomNormalShape = new SpriteAsset();
			zoomNormalShape.addEventListener(MouseEvent.MOUSE_DOWN, zoomNormalPanelSizeHandler);
			titleBar.addChild(zoomNormalShape);
			
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			// Create invisible rectangle to increase the hit area of the min icon.
			minShape.graphics.clear();
			minShape.graphics.lineStyle(0, 0, 0);
			minShape.graphics.beginFill(0xFFFFFF, 0.0);
			minShape.graphics.drawRect(unscaledWidth - 50, 12, 8, 8);
			
			// Draw min icon.
			minShape.graphics.lineStyle(2);
			minShape.graphics.beginFill(0xFFFFFF, 0.0);
			minShape.graphics.drawRect(unscaledWidth - 50, 18, 8, 2);
			
			// Draw restore icon.
			restoreShape.graphics.clear();
			restoreShape.graphics.lineStyle(2);
			restoreShape.graphics.beginFill(0xFFFFFF, 0.0);
			restoreShape.graphics.drawRect(unscaledWidth - 35, 12, 8, 8);
			restoreShape.graphics.moveTo(unscaledWidth - 35, 15);
			restoreShape.graphics.lineTo(unscaledWidth - 27, 15);
			
			// Draw close icon.
			closeShape.graphics.clear();
			closeShape.graphics.lineStyle(0, 0, 0);
			closeShape.graphics.drawRect(unscaledWidth - 20, 12, 8, 8);
			closeShape.graphics.lineStyle(2);
			closeShape.graphics.beginFill(0xFFFFFF, 0.0);
			closeShape.graphics.moveTo(unscaledWidth - 20, 12);
			closeShape.graphics.lineTo(unscaledWidth - 12, 20);
			closeShape.graphics.moveTo(unscaledWidth - 20, 20);
			closeShape.graphics.lineTo(unscaledWidth - 12, 12);
			
			//zoomMoins
			zoomMoinsShape.graphics.clear();
			zoomMoinsShape.graphics.lineStyle(2);
			zoomMoinsShape.graphics.beginFill(0xFFFFFF, 0.0);
			zoomMoinsShape.graphics.drawCircle(unscaledWidth - 110, 14, 6);
			zoomMoinsShape.graphics.moveTo(unscaledWidth - 112, 14);
			zoomMoinsShape.graphics.lineTo(unscaledWidth - 108, 14);
			
			//zoomNormal
			zoomNormalShape.graphics.clear();
			zoomNormalShape.graphics.lineStyle(2);
			zoomNormalShape.graphics.beginFill(0xFFFFFF, 0.0);
			zoomNormalShape.graphics.drawCircle(unscaledWidth - 95, 14, 6);
			
			//zoomPlus
			zoomPlusShape.graphics.clear();
			zoomPlusShape.graphics.lineStyle(2);
			zoomPlusShape.graphics.beginFill(0xFFFFFF, 0.0);
			zoomPlusShape.graphics.drawCircle(unscaledWidth - 80, 14, 6);
			zoomPlusShape.graphics.moveTo(unscaledWidth - 82, 14);
			zoomPlusShape.graphics.lineTo(unscaledWidth - 78, 14);
			zoomPlusShape.graphics.moveTo(unscaledWidth - 80, 12);
			zoomPlusShape.graphics.lineTo(unscaledWidth - 80, 16);
			
			// Draw resize graphics if not minimzed.				
			graphics.clear()
			if (isMinimized == false)
			{
				graphics.lineStyle(2);
				graphics.moveTo(unscaledWidth - 6, unscaledHeight - 1)
				graphics.curveTo(unscaledWidth - 3, unscaledHeight - 3, unscaledWidth - 1, unscaledHeight - 6);						
				graphics.moveTo(unscaledWidth - 6, unscaledHeight - 4)
				graphics.curveTo(unscaledWidth - 5, unscaledHeight - 5, unscaledWidth - 4, unscaledHeight - 6);						
			}
		}
		
		private var myRestoreHeight:int;
		private var isMinimized:Boolean = false; 
		
		// Minimize panel event handler.
		private function minPanelSizeHandler(event:Event):void
		{
			if (isMinimized != true)
			{
				myRestoreHeight = height;	
				height = titleBar.height;
				isMinimized = true;	
				// Don't allow resizing when in the minimized state.
				removeEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
			}				
		}
		
		// Restore panel event handler.
		private function restorePanelSizeHandler(event:Event):void
		{
			if (isMinimized == true)
			{
				height = myRestoreHeight;
				isMinimized = false;	
				// Allow resizing in restored state.				
				addEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
			}
		}
		
		private function closePanelSizeHandler(event:Event):void
		{
			(Canvas)(this.parent).removeChild(this);
			Application.application.tabPanelRooms.removeItemAt(Application.application.tabPanelRooms.getItemIndex(this));
			Application.application.arrangementPanelRooms(Application.application.tabPanelRooms);
		}
		
		private function zoomPlusPanelSizeHandler(event:Event):void
		{
			gdr.zoom("plusGrand");
		}
		
		private function zoomMoinsPanelSizeHandler(event:Event):void
		{
			gdr.zoom("plusPetit");
		}
		
		private function zoomNormalPanelSizeHandler(event:Event):void
		{
			gdr.zoom("normal");
			//gdr.setScale(0.5);
		}
			
		// Define static constant for event type.
		public static const RESIZE_CLICK:String = "resizeClick";
		
		// Resize panel event handler.
		public  function resizeHandler(event:MouseEvent):void
		{
			// Determine if the mouse pointer is in the lower right 7x7 pixel
			// area of the panel. Initiate the resize if so.
			
			// Lower left corner of panel
			var lowerLeftX:Number = x + width; 
			var lowerLeftY:Number = y + height;
			
			// Upper left corner of 7x7 hit area
			var upperLeftX:Number = lowerLeftX-7;
			var upperLeftY:Number = lowerLeftY-7;
			
			// Mouse positionin Canvas
			var panelRelX:Number = event.localX + x;
			var panelRelY:Number = event.localY + y;
			
			// See if the mousedown is in the lower right 7x7 pixel area
			// of the panel.
			if (upperLeftX <= panelRelX && panelRelX <= lowerLeftX)
			{
				if (upperLeftY <= panelRelY && panelRelY <= lowerLeftY)
				{		
					event.stopPropagation();		
					var rbEvent:MouseEvent = new MouseEvent(RESIZE_CLICK, true);
					// Pass stage coords to so all calculations using global coordinates.
					rbEvent.localX = event.stageX;
					rbEvent.localY = event.stageY;
					dispatchEvent(rbEvent);	
				}
			}				
		}		
	}
}